package gui;

import gui.component.PasswordInput;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import javax.swing.JOptionPane;
import org.apache.commons.codec.binary.Base64;
import security.AESKey;
import security.Hash;
import security.RSAKeyPair;

/**
 *
 * @author Tom
 */
public class UIConfig extends javax.swing.JDialog
{
    UIMain parent;    
    public UIConfig(UIMain parent) 
    {
        super(parent, true);
        initComponents();
        this.parent=parent;        
        initInterface();
        pack();
        setLocationRelativeTo(null);
    }
    
    private void initInterface() 
    {        
        txtName.setText(parent.user.Username);
        lbEmail.setText(parent.user.Email);
        txtEmailPassword.setText(parent.user.EmailPassword);
        txtIncomingServer.setText(parent.user.IncomingServer);
        cboxProtocol.setSelectedItem(parent.user.Protocol.equals("imaps")? "IMAP":"POP3");
        txtOutgoingServer.setText(parent.user.OutgoingServer);
        txtPort.setText(parent.user.Port);
        chboxSSL.setSelected(parent.user.SSL);  
        txtPublicKey.setText(parent.user.keypair.getPublicKeyBase64());
        txtPublicKey.setEditable(false);
        txtPublicKey.setLineWrap(true);
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        btEdit = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        btClearAll = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtPublicKey = new javax.swing.JTextArea();
        btCopyKey = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        btChage = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        txtCurrentPassword = new javax.swing.JPasswordField();
        txtNewPassword1 = new javax.swing.JPasswordField();
        txtNewPassword2 = new javax.swing.JPasswordField();
        jPanel3 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        txtIncomingServer = new javax.swing.JTextField();
        cboxProtocol = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtOutgoingServer = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        chboxSSL = new javax.swing.JCheckBox();
        txtPort = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        lbEmail = new javax.swing.JLabel();
        txtEmailPassword = new javax.swing.JPasswordField();
        btUpdateServerInfo = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configuration");

        jLabel1.setText("Votre nom");

        btEdit.setText("Mettre à jour");
        btEdit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEditActionPerformed(evt);
            }
        });

        jLabel2.setText("Remettre vos information *");

        btClearAll.setText("Effacer tous mes infos");
        btClearAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btClearAllActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel3.setText("<html><p align=\"justify\">* vos informations sont localement sauvegargées sur cette machine. Si vous ne l'utilisez plus, assurez que vos secrets sont protegés par les supprimer. Vos données existeront pourtant au serveur du fournisseur.</p></html>");
        jLabel3.setToolTipText("");

        jLabel14.setText("Votre cle publique");

        txtPublicKey.setColumns(20);
        txtPublicKey.setRows(5);
        jScrollPane1.setViewportView(txtPublicKey);

        btCopyKey.setText("Copier");
        btCopyKey.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCopyKeyActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel1)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btEdit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(btClearAll, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .addComponent(jLabel14)
                            .addComponent(jScrollPane1))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btCopyKey, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btEdit, javax.swing.GroupLayout.DEFAULT_SIZE, 29, Short.MAX_VALUE)
                    .addComponent(txtName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel14)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btCopyKey)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btClearAll, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jTabbedPane1.addTab("Infomations personnelles", jPanel1);

        jLabel4.setText("Mot de passe actuel");

        jLabel5.setText("Nouveau mot de passe");

        jLabel6.setText("Confirmer le mot de passe");

        btChage.setText("Changer");
        btChage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btChageActionPerformed(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        jLabel13.setText("<html><p align=\"justify\">* ce mot de passe-là ne sert qu'à gérer les informations locales. Distinguez-le avec celui de votre compte d'email que votre fournisseur vous offre</p></html>");
        jLabel13.setToolTipText("");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(121, Short.MAX_VALUE)
                .addComponent(btChage, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(110, 110, 110))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(txtCurrentPassword)
                    .addComponent(txtNewPassword1)
                    .addComponent(txtNewPassword2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtCurrentPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel5)
                .addGap(1, 1, 1)
                .addComponent(txtNewPassword1, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtNewPassword2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btChage, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Changer mot de passe", jPanel2);

        jLabel7.setText("Serveur entrant");

        cboxProtocol.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "IMAP", "POP3" }));

        jLabel8.setText("Protocole");

        jLabel9.setText("Serveur sortant");

        jLabel10.setText("Port");

        chboxSSL.setText("Secure Socket Layer (SSL) utilisé");

        jLabel11.setText("Adresse email");

        jLabel12.setText("Mot de passe");

        lbEmail.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N

        btUpdateServerInfo.setText("Mettre à jour");
        btUpdateServerInfo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUpdateServerInfoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtIncomingServer, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                    .addComponent(txtOutgoingServer, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtPort))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel11)
                        .addGap(18, 18, 18)
                        .addComponent(lbEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel12)
                        .addGap(18, 18, 18)
                        .addComponent(txtEmailPassword))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(cboxProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(chboxSSL, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(128, 128, 128)
                .addComponent(btUpdateServerInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 19, Short.MAX_VALUE)
                    .addComponent(lbEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtEmailPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 26, Short.MAX_VALUE)
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(txtIncomingServer, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(cboxProtocol, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel9)
                .addGap(4, 4, 4)
                .addComponent(txtOutgoingServer, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtPort, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chboxSSL)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btUpdateServerInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jTabbedPane1.addTab("Information du serveur", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 345, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btUpdateServerInfoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUpdateServerInfoActionPerformed
        boolean result=true;
        //result=result&&parent.user.databaseWorker.updateUserProperty("EmailPassword", new String(txtEmailPassword.getPassword()));
        String EmailPassword = new String(txtEmailPassword.getPassword());
        if (!EmailPassword.equals(parent.user.EmailPassword))
        {
            //encrypt email password before update to database
            result=result&&parent.user.databaseWorker.updateUserProperty("EmailPassword", parent.user.aesKey.encode(EmailPassword.getBytes()));
        }
        result=result&&parent.user.databaseWorker.updateUserProperty("IncomingServer", txtIncomingServer.getText());
        result=result&&parent.user.databaseWorker.updateUserProperty("Protocol", cboxProtocol.getSelectedItem().toString().equals("IMAP")? "imaps":"pop3");
        result=result&&parent.user.databaseWorker.updateUserProperty("OutgoingServer", txtOutgoingServer.getText());
        result=result&&parent.user.databaseWorker.updateUserProperty("Port", txtPort.getText());
        result=result&&parent.user.databaseWorker.updateUserProperty("SSL", chboxSSL.isSelected());
        if (result)
        {
            JOptionPane.showMessageDialog(parent, "Vos infos ont été mises à jour!","",JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog(parent, "Erreur se produit, operation terminée!","",JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_btUpdateServerInfoActionPerformed

    private void btChageActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btChageActionPerformed
                
        String currentPassword = new String(txtCurrentPassword.getPassword());
        String newPassword1 = new String(txtNewPassword1.getPassword());
        String newPassword2 = new String(txtNewPassword2.getPassword());

        if (!currentPassword.equals(parent.user.Password))
        {
            JOptionPane.showMessageDialog(parent, "Mot de passe actuel invalide!", "Erreur", JOptionPane.ERROR_MESSAGE);
            txtCurrentPassword.setText("");
            txtCurrentPassword.requestFocus();
        }
        else
        {
            if (!newPassword1.equals(newPassword2))
            {
                JOptionPane.showMessageDialog(parent, "Nouveux mots de passe ne sont pas identiques!", "Erreur", JOptionPane.ERROR_MESSAGE);
                txtNewPassword1.setText("");
                txtNewPassword2.setText("");
                txtNewPassword1.requestFocus();
            }
            else
            {
                Base64 base64 = new Base64();
                txtCurrentPassword.setText("");
                txtNewPassword1.setText("");
                txtNewPassword2.setText("");
                                    
                RSAKeyPair encryptKey;
                //generate new rsa key for encrypt aes
                encryptKey = new RSAKeyPair(newPassword2);                                
                
                //update
                //change the login password
                boolean result = true;
                result=result && parent.user.databaseWorker.updateUserProperty("Password", Hash.generateSHA256(newPassword2));
                //change the AESEncryptKey
                result=result && parent.user.databaseWorker.updateUserProperty("AESEncryptKey", base64.encodeToString(encryptKey.encodeByPublicKey(parent.user.aesKey.getKeyEncoded())));
                
                if (result)
                {
                    JOptionPane.showMessageDialog(parent, "Mot de passe a été mis à jour!","",JOptionPane.INFORMATION_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(parent, "Erreur se produit, operation terminée!","",JOptionPane.ERROR_MESSAGE);
                }

            }
        }
    }//GEN-LAST:event_btChageActionPerformed

    private void btClearAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btClearAllActionPerformed
        btClearAll.setEnabled(false);
        int result=JOptionPane.showConfirmDialog(parent, "Cette action ne peut pas être défait. Continuer?", "Confirmer de suppression", JOptionPane.YES_NO_OPTION);
        if (result==JOptionPane.YES_OPTION)
        {
            String password = PasswordInput.showInput(parent);//JOptionPane.showIn(parent, "Saisissez votre mot de passe actuel", "");
            if (password!=null)
            {
                if (parent.user.Password.equals(password))
                {
                    boolean success = parent.user.databaseWorker.deleteAllInfo();
                    if (success)
                    {
                        JOptionPane.showMessageDialog(parent, "Opération completée!","",JOptionPane.INFORMATION_MESSAGE);
                        //log out
                        this.setVisible(false);
                        UILogin log = new UILogin();
                        log.setVisible(true);
                        this.dispose();
                        parent.dispose();
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(parent, "Erreur se produit, opération terminée!","",JOptionPane.INFORMATION_MESSAGE);
                        btClearAll.setEnabled(true);
                    }
                }
                else
                {
                    JOptionPane.showMessageDialog(parent, "Mot de passe invalide, action annulée!", "", JOptionPane.INFORMATION_MESSAGE);
                    btClearAll.setEnabled(true);
                }
            }
            else
            {
                btClearAll.setEnabled(true);
            }
        }
    }//GEN-LAST:event_btClearAllActionPerformed

    private void btEditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btEditActionPerformed
        String newName = txtName.getText().trim();
        if ("".equals(newName))
        {
            JOptionPane.showMessageDialog(this, "Saisissez votre nom!","",JOptionPane.INFORMATION_MESSAGE);
            txtName.setText(parent.user.Username);
            txtName.requestFocus();
            txtName.selectAll();
        }
        else
        {
            boolean result = parent.user.databaseWorker.updateUserProperty("Username",newName);
            if (result)
            {
                JOptionPane.showMessageDialog(this, "Votre nom a été mis à jour!");
                txtName.setText(newName);
                parent.updateName();
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Erreur se produit en mettant à jour la base de données!");
                txtName.setText(parent.user.Username);
            }
        }
    }//GEN-LAST:event_btEditActionPerformed

    private void btCopyKeyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCopyKeyActionPerformed
                
        //copy text to clipborad
        StringSelection stringSelection = new StringSelection(txtPublicKey.getText());
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
        JOptionPane.showMessageDialog(this, "Cle copiee au clipboard", "", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_btCopyKeyActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btChage;
    private javax.swing.JButton btClearAll;
    private javax.swing.JButton btCopyKey;
    private javax.swing.JButton btEdit;
    private javax.swing.JButton btUpdateServerInfo;
    private javax.swing.JComboBox cboxProtocol;
    private javax.swing.JCheckBox chboxSSL;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel lbEmail;
    private javax.swing.JPasswordField txtCurrentPassword;
    private javax.swing.JPasswordField txtEmailPassword;
    private javax.swing.JTextField txtIncomingServer;
    private javax.swing.JTextField txtName;
    private javax.swing.JPasswordField txtNewPassword1;
    private javax.swing.JPasswordField txtNewPassword2;
    private javax.swing.JTextField txtOutgoingServer;
    private javax.swing.JTextField txtPort;
    private javax.swing.JTextArea txtPublicKey;
    // End of variables declaration//GEN-END:variables
}
